#!/usr/bin/env bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2018-2019, Intel Corporation
#
#
# pmempool_sync/TEST25 -- test for checking pmempool sync
#
# sync with a poolset with incorrect declared size of a device-dax-based damaged
# part, also in dry-run mode
# Ref: pmem/issues#805
#

. ../unittest/unittest.sh

require_test_type medium
require_fs_type any
require_dax_devices 2

# the test takes too long under pmemcheck
configure_valgrind pmemcheck force-disable

setup

dax_device_zero

LOG=out${UNITTEST_NUM}.log
LOG_TEMP=out${UNITTEST_NUM}_part.log
rm -f $LOG && touch $LOG
rm -f $LOG_TEMP && touch $LOG_TEMP

LAYOUT=OBJ_LAYOUT$SUFFIX
POOLSET1=$DIR/poolset1
POOLSET2=$DIR/poolset2

# Create poolset files
create_poolset $POOLSET1 \
	AUTO:${DEVICE_DAX_PATH[0]} \
	r \
	AUTO:${DEVICE_DAX_PATH[1]}

create_poolset $POOLSET2 \
	AUTO:${DEVICE_DAX_PATH[0]} \
	r \
	1500000000:${DEVICE_DAX_PATH[1]}

# Create a poolset
expect_normal_exit $PMEMPOOL$EXESUFFIX create --layout=$LAYOUT obj $POOLSET1
cat $LOG >> $LOG_TEMP

# Damage the replica
expect_normal_exit $PMEMPOOL$EXESUFFIX rm ${DEVICE_DAX_PATH[1]}

# Try to synchronize replicas using the second poolset in dry-run mode
expect_abnormal_exit $PMEMPOOL$EXESUFFIX sync -d $POOLSET2 >> $LOG_TEMP 2>&1

# Try to synchronize replicas using the second poolset in normal mode
expect_abnormal_exit $PMEMPOOL$EXESUFFIX sync $POOLSET2 >> $LOG_TEMP 2>&1

# Synchronize replicas using the first poolset
expect_normal_exit $PMEMPOOL$EXESUFFIX sync $POOLSET1 >> $LOG_TEMP

# Check metadata using pmempool info
dump_pool_info $POOLSET1 >> $LOG_TEMP

mv $LOG_TEMP $LOG
check

pass
